package com.zw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.constant.MessageConstant;
import com.zw.dto.DirectoryDto;
import com.zw.dto.ProjectDto;
import com.zw.entity.Directory;
import com.zw.exception.base.BaseException;
import com.zw.mapper.DirectoryMapper;
import com.zw.service.DirectoryService;
import com.zw.service.ProjectService;
import com.zw.utils.SecurityUtils;
import com.zw.vo.DirectoryVo;
import com.zw.vo.ProjectVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
public class DirectoryServiceImpl extends ServiceImpl<DirectoryMapper, Directory> implements DirectoryService {

    @Resource
    private ProjectService projectService;

    @Override
    public List<DirectoryVo> directoryList(DirectoryDto directoryDto) {
        Long userId = SecurityUtils.getUserId();
        // 查询用户的所有目录
        LambdaQueryWrapper<Directory> wq = new LambdaQueryWrapper<>();
        wq.eq(Directory::getUserId, userId);
        List<Directory> directories = baseMapper.selectList(wq);
        List<DirectoryVo> directoryVos = BeanUtil.copyToList(directories, DirectoryVo.class);
        // 取出所有的顶层目录
        List<DirectoryVo> parentDirectory = directoryVos.stream().filter(directoryVo -> Objects.equals(directoryVo.getParentId(), directoryDto.getParentId())).toList();
        // 获取子目录
        parentDirectory.forEach(root -> getSubDirectory(directoryVos, root));
        return parentDirectory;
    }

    @Override
    public void removeDirectory(Long id) {
        // 是否有子目录
        LambdaQueryWrapper<Directory> wq = new LambdaQueryWrapper<>();
        wq.eq(Directory::getParentId, id);
        if (baseMapper.selectCount(wq) > 0) {
            throw new BaseException(MessageConstant.DIRECTORY_NOT_EMPTY);
        }
        // 是否有项目
        ProjectDto projectDto = new ProjectDto();
        projectDto.setDirectoryId(id);
        List<ProjectVo> projectVos = projectService.projectList(projectDto);
        if (projectVos.size() > 0) {
            throw new BaseException(MessageConstant.PROJECT_NOT_EMPTY);
        }
        baseMapper.deleteById(id);
    }


    @Override
    public List<Directory> selectDirectoryList(Directory directory) {
        return baseMapper.selectDirectoryList(directory);
    }

    @Override
    public Directory selectDirectoryId(Long id) {
        List<Directory> directories = baseMapper.selectDirectoryList(Directory.builder().id(id).build());
        return CollectionUtils.isEmpty(directories) ? null : directories.get(0);
    }

    /**
     * 获取目录子树
     */
    private void getSubDirectory(List<DirectoryVo> directoryVos, DirectoryVo root) {
        // 获取子目录
        List<DirectoryVo> subDirectory = directoryVos.stream()
                .filter(d -> root.getId().equals(d.getParentId()))
                .toList();
        if (subDirectory.size() > 0) {
            root.setChildren(subDirectory);
            // 递归处理每个子目录
            for (DirectoryVo subDir : subDirectory) {
                getSubDirectory(directoryVos, subDir);
            }
        }
    }


}
